package org.ft.fileConvert.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import org.ft.fileConvert.config.FilePathConfig;
import org.ft.fileConvert.entity.FileConvert;
import org.ft.fileConvert.mapper.FileConvertMapper;
import org.ft.fileConvert.service.FileConvertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ft.fileConvert.utils.FileConvertUtil;
import org.ft.fileConvert.vo.ResultVo;
import org.ofdrw.converter.ConvertHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chentianzhong
 * @since 2022-08-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FileConvertServiceImpl extends ServiceImpl<FileConvertMapper, FileConvert> implements FileConvertService {

    @Resource
    private FilePathConfig filePathConfig;
    @Resource
    private FileConvertUtil fileConvertUtil;

    @Override
    public FileConvert upload(MultipartFile file , Integer convertType) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
        String format = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
        File path = new File(filePathConfig.getLocalPath()+format);
        if (!path.exists()){
            path.mkdirs();
        }
        String localFilePath = path.getAbsolutePath()+File.separator+ IdUtil.fastSimpleUUID()+ext;
        FileUtil.writeBytes(file.getBytes(), localFilePath);

        FileConvert fileConvert = FileConvert.builder()
                .originalName(originalFilename)
                .originalPath(localFilePath)
                .originalSize(file.getSize())
                .convertType(convertType)
                .createTime(new Date())
                .build();

        this.save(fileConvert);
        return fileConvert;
    }

    @Override
    public ResultVo convert(String id) throws Exception {
        FileConvert fileConvert = this.getById(id);
        if (fileConvert == null){
            return ResultVo.fail("获取转化文件对象失败");
        }
        String originalName = fileConvert.getOriginalName();
        String originalFileName = originalName.substring(0, originalName.lastIndexOf("."));
        String format = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
        File tempPath = new File(filePathConfig.getTempPath()+format);
        if (!tempPath.exists()){
            tempPath.mkdirs();
        }
        File finalPath = new File(filePathConfig.getFinalPath()+format);
        if (!finalPath.exists()){
            finalPath.mkdirs();
        }

        switch (fileConvert.getConvertType()){
            case 1: //word转pdf
                String pdfFilePath = finalPath.getAbsolutePath()+File.separator+ IdUtil.fastSimpleUUID()+".pdf";
                fileConvertUtil.convertWordToPdf(fileConvert.getOriginalPath(), pdfFilePath);
                fileConvert.setFinalName(originalFileName+".pdf");
                fileConvert.setFinalPath(pdfFilePath);
                this.updateById(fileConvert);
                break;
            case 2: //word转ofd
                String tempFilePath = tempPath.getAbsolutePath()+File.separator+ IdUtil.fastSimpleUUID()+".pdf";
                String ofdFilePath = finalPath.getAbsolutePath()+File.separator+ IdUtil.fastSimpleUUID()+".ofd";
                fileConvertUtil.convertWordToPdf(fileConvert.getOriginalPath(), tempFilePath);
                fileConvertUtil.convertPdfToOfd(tempFilePath, ofdFilePath);
                fileConvert.setTempName(originalFileName+".pdf");
                fileConvert.setTempPath(tempFilePath);
                fileConvert.setFinalName(originalFileName+".ofd");
                fileConvert.setFinalPath(ofdFilePath);
                this.updateById(fileConvert);
                break;
            case 3: //pdf转ofd
                String ofdFilePath1 = finalPath.getAbsolutePath()+File.separator+ IdUtil.fastSimpleUUID()+".ofd";
                fileConvertUtil.convertPdfToOfd(fileConvert.getOriginalPath(), ofdFilePath1);
                fileConvert.setFinalName(originalFileName+".ofd");
                fileConvert.setFinalPath(ofdFilePath1);
                this.updateById(fileConvert);
                break;
            case 4: //ofd转pdf
                String pdfFilePath1 = finalPath.getAbsolutePath()+File.separator+ IdUtil.fastSimpleUUID()+".pdf";
                ConvertHelper.toPdf(Paths.get(fileConvert.getOriginalPath()), Paths.get(pdfFilePath1));
                fileConvert.setFinalName(originalFileName+".pdf");
                fileConvert.setFinalPath(pdfFilePath1);
                this.updateById(fileConvert);
                break;
            default:
                return ResultVo.fail("暂未开放此类文件转换");
        }
        return ResultVo.success(fileConvert);
    }

    @Override
    public void downloadNet(String url) {

    }
}
